package task

import (
	"cvevulner/common"
	"cvevulner/taskhandler"
	"errors"
	"github.com/astaxie/beego/config"
	"github.com/astaxie/beego/logs"
)

//ProcCveOriginData Process raw data obtained by api
func ProcCveOriginData(prcNum, days, credibilityLevel, openeulerNum int, cveRef, owner string) (bool, error) {
	// Process raw data obtained by api
	ok, err := taskhandler.GetCveOriginData(prcNum, days, openeulerNum, credibilityLevel, cveRef)
	if !ok {
		logs.Error("ProcCveOriginData, GetCveOriginData, err: ", err)
	}
	// Processing excel raw data
	okx, err := taskhandler.GetCveOriginExcelData(prcNum, days, openeulerNum, cveRef)
	if !okx {
		logs.Error("ProcCveOriginData, GetCveOriginExcelData, err: ", err)
	}
	return true, nil
}

//ParamsCveOriginData Processing cve raw data
func ParamsCveOriginData() error {
	defer common.Catchs()
	logs.Info("Start the task of generating cve raw data into cve library.")
	// Query the cve to be processed, 1: add; 2: modify
	BConfig, err := config.NewConfig("ini", "conf/app.conf")
	if err != nil {
		logs.Error("config init, error: ", err)
		return err
	}
	// The amount of data processed at a time
	prcnum, err := BConfig.Int("crontab::prcnum")
	if err != nil {
		logs.Error("crontab::prcnum, error:  ", err)
		return errors.New("value is nil")
	}
	// How many days have been processed to date data
	days, ok := BConfig.Int("crontab::days")
	if ok != nil {
		logs.Error("crontab::days, err:", err)
		return ok
	}
	// openeuler Number start value
	cveRef := BConfig.String("cve::cveref")
	openeulernum, ok := BConfig.Int("cve::openeulernum")
	if ok != nil {
		logs.Error("cve::openeulernum, err:", err)
		return ok
	}
	owner := BConfig.String("gitee::owner")
	// cve credibility level
	credibilityLevel, ok := BConfig.Int("cve::credibility_level")
	if ok != nil {
		logs.Error("cve::credibility_level, err:", err)
		return ok
	}
	// Get the data source of the table
	_, errx := ProcCveOriginData(prcnum, days, credibilityLevel, openeulernum, cveRef, owner)
	logs.Info("The task of generating cve original data into cve library is over.")
	return errx
}
